package io.renren.modules.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.member.entity.TStoreMemberEntity;
import io.renren.modules.member.entity.TStoreMemberTypeEntity;
import io.renren.modules.member.service.TStoreMemberService;
import io.renren.modules.member.service.TStoreMemberTypeService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-26 22:36:10
 */
@RestController
@RequestMapping("store/tstoremembertype")
public class TStoreMemberTypeController extends AbstractController {
    @Autowired
    private TStoreMemberTypeService tStoreMemberTypeService;
    @Autowired
    private TStoreMemberService storeMemberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoremembertype:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("status",params.get("status"));
        params.put("sellerId",getSeller());
        PageUtils page = tStoreMemberTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @NoRepeatSubmit
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstoremembertype:info")
    public R info(@PathVariable("id") Long id){
		TStoreMemberTypeEntity tStoreMemberType = tStoreMemberTypeService.getById(id);

        return R.ok().put("tStoreMemberType", tStoreMemberType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoremembertype:save")
    public R save(@RequestBody TStoreMemberTypeEntity tStoreMemberType){
        TStoreMemberTypeEntity one = tStoreMemberTypeService.getOne(new QueryWrapper<TStoreMemberTypeEntity>()
                .eq("seller_id", getSeller())
                .eq("member_type_name", tStoreMemberType.getMemberTypeName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("会员类型已存在，请重新输入");
        }
        tStoreMemberType.setSellerId(getSeller());
		tStoreMemberTypeService.save(tStoreMemberType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoremembertype:update")
    public R update(@RequestBody TStoreMemberTypeEntity tStoreMemberType){
		tStoreMemberTypeService.updateById(tStoreMemberType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoremembertype:delete")
    public R delete(@RequestBody Long[] ids){
        TStoreMemberEntity one = storeMemberService.getOne(new QueryWrapper<TStoreMemberEntity>()
                .eq("seller_id", getSeller())
                .eq("member_type_id", ids[0])
        );
        if (one != null){
            throw new RRException("此会员类型下存在会员，不能删除");
        }
        tStoreMemberTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
